package asu.command
{
	import asu.util.StringUtil;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class LoadURLCommand extends EventDispatcher
	{
		public var caller			:Object;
		
		protected var _url			:String;
		protected var _loader		:URLLoader;
		protected var _nocache		:Boolean;
		
		public function LoadURLCommand(target:IEventDispatcher=null)
		{
			super(target);
			init();
		}
		
		public function get url():String
		{
			return _url;
		}

		public function get loader():URLLoader
		{
			return _loader;
		}
		
		public function get nocache():Boolean
		{
			return _nocache;
		}
		
		public function set nocache(value:Boolean):void
		{
			_nocache = value;
		}
		
		public function release():void
		{
			removeListeners( _loader );
			_loader = null;
		}
		
		public function loadURL( url:String, useNoCache:Boolean=false ):void
		{
			_load( url, useNoCache );
		}
		
		protected function _load( url:String, useNoCache:Boolean=false ):void
		{
			_url = url;
			
			if( nocache || useNoCache ) url += '?nocache='+StringUtil.generateRandomString( 8 );
			
			try
			{
				_loader.load( new URLRequest( url ) );
			}
			catch( error:Error )
			{
				trace("Load Error: " + error);
				handleError( error.message );
			}
		}
		
		protected function init():void
		{
			_loader = new URLLoader();
			addListeners( _loader );
		}
		
		protected function addListeners(dispatcher:IEventDispatcher):void 
		{
			dispatcher.addEventListener( Event.COMPLETE, completeHandler );
			dispatcher.addEventListener( Event.OPEN, openHandler );
			dispatcher.addEventListener( ProgressEvent.PROGRESS, progressHandler );
			dispatcher.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
			dispatcher.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
			dispatcher.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
		}
		
		protected function removeListeners(dispatcher:IEventDispatcher):void 
		{
			dispatcher.removeEventListener( Event.COMPLETE,completeHandler );
			dispatcher.removeEventListener( Event.OPEN, openHandler );
			dispatcher.removeEventListener( ProgressEvent.PROGRESS, progressHandler );
			dispatcher.removeEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
			dispatcher.removeEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
			dispatcher.removeEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
		}
		
		protected function handleError( error:String ):void
		{
			dispatchEvent( new ErrorEvent( ErrorEvent.ERROR, false, false, error ) );
		}
		
		protected function completeHandler( event:Event ):void 
		{
			dispatchEvent( event );
		}
		
		protected function ioErrorHandler(event:IOErrorEvent):void 
		{
			trace("ioErrorHandler: " + event);
			handleError( event.text );
		}
		
		protected function securityErrorHandler(event:SecurityErrorEvent):void 
		{
			trace("securityErrorHandler: " + event);
			handleError( event.text );
		}
		
		protected function openHandler(event:Event):void 
		{
			//trace("openHandler: " + event);
		}
		
		protected function progressHandler(event:ProgressEvent):void 
		{
			//trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
		}
		
		protected function httpStatusHandler(event:HTTPStatusEvent):void 
		{
			//trace("httpStatusHandler: " + event);
		}
	}
}